<?php


namespace App\Libs;


use App\Exceptions\AppException;

/**
 * 验证类
 *
 * Class Check
 *
 * @package App\Libs
 */
class Check
{
    const MSG_DEFAULT = '系统繁忙';

    private function __construct() {}

    private function __clone() {}


    /**
     * 异常抛出
     *
     * @param $msg
     * @param int $code
     *
     * @throws AppException
     */
    private static function throw($msg, $code = AppException::ERR_SYSTEM_BUSY)
    {
        throw new AppException($msg, $code);
    }

    /**
     * 验证null
     *
     * @param $val
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkNull($val, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if ($val === null) self::throw($msg, $code);
    }

    /**
     * 验证非null
     *
     * @param $val
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkNotNull($val, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if ($val !== null) self::throw($msg, $code);
    }

    /**
     * 验证存在性
     *
     * @param $val
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkExists($val, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if (!isset($val)) self::throw($msg, $code);
    }

    /**
     * 验证存在且非空
     *
     * @param $val
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkBoolean($val, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if (!isset($val) || !$val) self::throw($msg, $code);
    }

    /**
     * 验证是否存在于数组中
     *
     * @param $val
     * @param array $arr
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkInArr($val, array $arr, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if (!$val || !in_array($val, $arr)) self::throw($msg, $code);
    }

    /**
     * 验证非空数组
     *
     * @param $arr
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkEmptyArr($arr, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if (!is_array($arr) || empty($arr)) self::throw($msg, $code);
    }

    /**
     * 验证int相等
     *
     * @param int $current_val
     * @param int $check_val
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkIntEqual(int $current_val, int $check_val, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if ($current_val !== $check_val) self::throw($msg, $code);
    }

    /**
     * 验证字符串长度
     *
     * @param $string
     * @param $length
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkStringLength($string, $length, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        if (strlen($string) > $length) self::throw($msg, $code);
    }

    /**
     * 验证手机号
     *
     * @param $mobile
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkMobile($mobile, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        $match = '/^(1(([356789][0-9])|(47)))\d{8}$/';
        if (!preg_match($match, $mobile)) {
            self::throw($msg, $code);
        }
    }

    /**
     * 验证IP
     *
     * @param $ip
     * @param string $msg
     * @param int $code
     *
     * @throws AppException
     */
    public static function checkIp($ip, $msg = self::MSG_DEFAULT, $code = AppException::ERR_SYSTEM_BUSY)
    {
        $match = '/^(((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))$/';
        if (!preg_match($match, $ip)) {
            self::throw($msg, $code);
        }
    }

}
